package com.esgov.chatbot.api;

import com.esgov.chatbot.service.TProWoFormService;
import com.esgov.chatbot.support.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工单统计接口控制器
 */
@RestController
@RequestMapping("/api/workorder")
public class TProWoFormController {

    @Autowired
    private TProWoFormService workOrderService;

    /**
     * 获取工单总数
     * @return 工单总数
     */
    @GetMapping("/count")
    public ResponseResult<Long> getWorkOrderCount() {
        long count = workOrderService.countAllWorkOrders();
        return ResponseResult.success(count);
    }
    
    /**
     * 获取标签不为空的工单数
     * @return 标签不为空的工单数
     */
    @GetMapping("/label-count")
    public ResponseResult<Long> getWorkOrderLabelCount() {
        long count = workOrderService.countByLabelNotNull();
        return ResponseResult.success(count);
    }
    
    /**
     * 获取预警工单数(黄牌+红牌)
     * @return 预警工单数
     */
    @GetMapping("/warning-count")
    public ResponseResult<Long> getWorkOrderWarningCount() {
        long count = workOrderService.countBySupervisionWarning();
        return ResponseResult.success(count);
    }
    
    /**
     * 根据区域地址分组统计工单数
     * @return 区域统计数据
     */
    @GetMapping("/area-group")
    public ResponseResult<List<Map<String, Object>>> getWorkOrderByAreaGroup() {
        List<Map<String, Object>> data = workOrderService.countByAreaGroup();
        return ResponseResult.success(data);
    }
    
    /**
     * 根据工单标题分组统计工单数
     * @return 话题量统计数据
     */
    @GetMapping("/title-group")
    public ResponseResult<List<Map<String, Object>>> getWorkOrderByTitleGroup() {
        List<Map<String, Object>> data = workOrderService.countByTitleGroup();
        return ResponseResult.success(data);
    }
    
    /**
     * 根据工单事项分组统计工单数
     * @return 事件量统计数据
     */
    @GetMapping("/act-group")
    public ResponseResult<List<Map<String, Object>>> getWorkOrderByActGroup() {
        List<Map<String, Object>> data = workOrderService.countByActNameGroup();
        return ResponseResult.success(data);
    }
    
    /**
     * 获取所有统计数据
     * @return 所有统计数据
     */
    @GetMapping("/statistics")
    public ResponseResult<Map<String, Object>> getAllStatistics() {
        Map<String, Object> result = new HashMap<>();
        result.put("total", workOrderService.countAllWorkOrders());
        result.put("labelCount", workOrderService.countByLabelNotNull());
        result.put("warningCount", workOrderService.countBySupervisionWarning());
        result.put("areaGroup", workOrderService.countByAreaGroup());
        result.put("titleGroup", workOrderService.countByTitleGroup());
        result.put("actGroup", workOrderService.countByActNameGroup());
        return ResponseResult.success(result);
    }
}